package city.spring.modules.medicalstation.medical.controller;

import city.spring.modules.medicalstation.medical.entity.MedicalRecordEntity;
import city.spring.modules.medicalstation.medical.service.MedicalRecordService;
import city.spring.modules.medicalstation.medical.vo.MedicalRecordEntityVO;
import city.spring.modules.oldman.service.OldmanInfoService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：
 *
 * @author
 * @date 2020-06-17 14:23:13
 */
@RestController
@RequestMapping("/medical_record")
public class MedicalRecordController extends ApiController {
    private final MedicalRecordService medicalRecordService;
    private final OldmanInfoService oldmanInfoService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<MedicalRecordEntity, ?>> entityOrderFields;

    public MedicalRecordController(MedicalRecordService medicalRecordService, OldmanInfoService oldmanInfoService) {
        this.medicalRecordService = medicalRecordService;
        this.oldmanInfoService = oldmanInfoService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(MedicalRecordEntity::getId);
        entityOrderFields.add(MedicalRecordEntity::getPerson);
        entityOrderFields.add(MedicalRecordEntity::getSeeTime);
        entityOrderFields.add(MedicalRecordEntity::getDoctor);
        entityOrderFields.add(MedicalRecordEntity::getHospital);
        entityOrderFields.add(MedicalRecordEntity::getDiagnostic);
        entityOrderFields.add(MedicalRecordEntity::getAdvice);
        entityOrderFields.add(MedicalRecordEntity::getGmtCreate);
        entityOrderFields.add(MedicalRecordEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong></strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, MedicalRecordEntity entity) {
        LambdaQueryChainWrapper<MedicalRecordEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong></strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, MedicalRecordEntityVO entity) {
        QueryWrapper<? extends MedicalRecordEntity> queryWrapper = buildQuery(entity);
        return success(medicalRecordService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    /**
     * 获取一个 <strong></strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        Page<MedicalRecordEntityVO> page = new Page<>();
        QueryWrapper<MedicalRecordEntityVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        IPage<MedicalRecordEntityVO> iPage = medicalRecordService.listPage(page, wrapper);
        List<MedicalRecordEntityVO> records = iPage.getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
    }

    /**
     * 添加一个 <strong></strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody MedicalRecordEntity entity) {
        medicalRecordService.saveHealthSeeDoctor(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong></strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody MedicalRecordEntity entity) {
        entity.setId(id);
        medicalRecordService.updateHealthSeeDoctor(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong></strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        medicalRecordService.deleteHealthSeeDoctor(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong></strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        medicalRecordService.deleteHealthSeeDoctor(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<MedicalRecordEntity> buildLambdaQuery(MedicalRecordEntity entity) {
        LambdaQueryChainWrapper<MedicalRecordEntity> lambdaQuery = medicalRecordService.lambdaQuery();
        lambdaQuery.eq(entity.getId() != null, MedicalRecordEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPerson()), MedicalRecordEntity::getPerson, entity.getPerson());
        lambdaQuery.eq(entity.getSeeTime() != null, MedicalRecordEntity::getSeeTime, entity.getSeeTime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDoctor()), MedicalRecordEntity::getDoctor, entity.getDoctor());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getHospital()), MedicalRecordEntity::getHospital, entity.getHospital());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDiagnostic()), MedicalRecordEntity::getDiagnostic, entity.getDiagnostic());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAdvice()), MedicalRecordEntity::getAdvice, entity.getAdvice());
        lambdaQuery.eq(entity.getGmtCreate() != null, MedicalRecordEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, MedicalRecordEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<? extends MedicalRecordEntity> buildQuery(MedicalRecordEntityVO entity) {
        QueryWrapper<? extends MedicalRecordEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getPerson()), "t1.person", entity.getPerson());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanName()), "t2.oldman_name", entity.getOldmanName());
        wrapper.eq(entity.getSeeTime() != null, "t1.see_time", entity.getSeeTime());
        wrapper.like(StringUtils.isNotBlank(entity.getDoctor()), "t1.doctor", entity.getDoctor());
        wrapper.like(StringUtils.isNotBlank(entity.getHospital()), "t1.hospital", entity.getHospital());
        wrapper.like(StringUtils.isNotBlank(entity.getDiagnostic()), "t1.diagnostic", entity.getDiagnostic());
        wrapper.like(StringUtils.isNotBlank(entity.getAdvice()), "t1.advice", entity.getAdvice());
        wrapper.eq(entity.getGmtCreate() != null, "t1.gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "t1.gmt_modified", entity.getGmtModified());


        return wrapper;
    }

}
